package com.senyin.servlet.dept;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.senyin.common.BaseResponse;
import com.senyin.common.ErrorCode;
import com.senyin.common.ResultUtils;
import com.senyin.entity.Dept;
import com.senyin.service.DeptService;
import com.senyin.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class DeptModServlet extends HttpServlet {
    private DeptService deptService = new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String dpId = req.getParameter("dpId");
        String dpName = req.getParameter("dpName");
        String dpDes = req.getParameter("dpDes");
        Dept dept = new Dept(dpId,dpName, dpDes);
        BaseResponse baseResponse = null;
        try {
            baseResponse = deptService.modDept(dept, req);
        } catch (SQLException e) {
            baseResponse = ResultUtils.error(ErrorCode.SYSTEM_ERROR,"error");
            resp.getWriter().write(objectMapper.writeValueAsString(baseResponse));
        }
        resp.getWriter().write(objectMapper.writeValueAsString(baseResponse));
    }
}
